﻿using personalplaner.common.mvvm.common;
using System.Windows;
using personalplaner.common.mvvm.mediator;
using personalplaner.common.services;

namespace personalplaner.common.mvvm
{
    /// <summary>
    /// Base class for all view models
    /// </summary>
    public class BaseViewModel : NotifyPropertyChangedBase
    {
        public BaseViewModel(DependencyObject parent)
        {
            if (Designer.IsDesignMode)
                return;

            Parent = parent;

			//Register all decorated methods to the Mediator
			Mediator.Register(this);
        }

        public DependencyObject Parent
        {
            get;
            private set;
        }

        public virtual void Loaded()
        {
        }

		public virtual void Unload()
		{
		}

    	#region ServiceLocator

        ServiceLocator serviceLocator = new ServiceLocator();

        /// <summary>
        /// Gets the service locator 
        /// </summary>
        public ServiceLocator ServiceLocator
        {
            get 
            {
                return serviceLocator; 
            }
        }

        /// <summary>
        /// Gets a service from the service locator
        /// </summary>
        /// <typeparam name="T">The type of service to return</typeparam>
        /// <returns>Returns a service that was registered with the Type T</returns>
        public T GetService<T>()
        {
            return serviceLocator.GetService<T>();
        }

        /// <summary>
        /// gets a service from the servicelocator. 
        /// if the service doesn't exist a new instance is created
        /// </summary>
        /// <typeparam name="T">type of service you want to get</typeparam>
        /// <typeparam name="O">type of instance that will be registered with the type</typeparam>
        /// <returns></returns>
        public T GetService<T, O>() where O : class, new()
        {
            return serviceLocator.GetService<T, O>();
        }

        #endregion

		#region Mediator

		static readonly Mediator mediator = new Mediator();

		public Mediator Mediator
		{
			get { return mediator; }
		}

		#endregion

        #region MessageBox

        protected IMessageBoxService MessageBox
        {
            get
            {
                return GetService<IMessageBoxService>();
            }
        }

        #endregion

    }
}
